package org.jeecg.modules.demo.yxqcysOrder.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.pubuyorder.service.IPuBuyOrderService;
import org.jeecg.modules.demo.utils.generateOddUtil;
import org.jeecg.modules.demo.yxqcysOrder.entity.YxQcysOrder;
import org.jeecg.modules.demo.yxqcysOrder.service.IYxQcysOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 期初应收款单
 * @Author: jeecg-boot
 * @Date:   2023-03-15
 * @Version: V1.0
 */
@Api(tags="期初应收款单")
@RestController
@RequestMapping("/yxqcysOrder/yxQcysOrder")
@Slf4j
public class YxQcysOrderController extends JeecgController<YxQcysOrder, IYxQcysOrderService> {
	@Autowired
	private IYxQcysOrderService yxQcysOrderService;
	@Autowired
	private IPuBuyOrderService puBuyOrderService;
	/**
	 * 分页列表查询
	 *
	 * @param yxQcysOrder
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "期初应收款单-分页列表查询")
	@ApiOperation(value="期初应收款单-分页列表查询", notes="期初应收款单-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="yunxiao/yxqcysOrder/YxQcysOrderList")
	public Result<?> queryPageList(YxQcysOrder yxQcysOrder,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<YxQcysOrder> queryWrapper = QueryGenerator.initQueryWrapper(yxQcysOrder, req.getParameterMap());
		Page<YxQcysOrder> page = new Page<YxQcysOrder>(pageNo, pageSize);
		IPage<YxQcysOrder> pageList = yxQcysOrderService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param yxQcysOrder
	 * @return
	 */
	@AutoLog(value = "期初应收款单-添加")
	@ApiOperation(value="期初应收款单-添加", notes="期初应收款单-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody YxQcysOrder yxQcysOrder,HttpServletRequest req) {
		try{
			String tenantId = req.getHeader("tenant-id");
			String precode = puBuyOrderService.getTeantCodeById(Integer.parseInt(tenantId)).toUpperCase();
			String str = yxQcysOrderService.getMaxCode();
			Map<String,String> map = new HashMap<>();
			map.put("maxCode", str==null?null:str);
			String name =precode+ "QCYS_";
			String resultCode = generateOddUtil.createCode(map,name);
			yxQcysOrder.setQcysNo(resultCode);
			yxQcysOrder.setShenhe(0);
			yxQcysOrder.setCreateTime(new Date());
			yxQcysOrder.setSurplusJine(yxQcysOrder.getQcysje());
			yxQcysOrderService.save(yxQcysOrder);
			return Result.OK("添加成功！");
		}catch (Exception e){
			return Result.error(500,"系统错误！");
		}
	}
	/**
	 *  审核
	 *
	 * @param yxQcysOrder
	 * @return
	 */
	@AutoLog(value = "期初应收款单-审核")
	@ApiOperation(value="期初应收款单-审核", notes="期初应收款单-审核")
	@PostMapping(value = "/checkYsOrder")
	public Result<?> checkYsOrder(@RequestBody  YxQcysOrder yxQcysOrder) {
		try{
			// 获取登录用户信息
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			if(!sysUser.getId().equals(yxQcysOrder.getSaleman())){
				return Result.error(500,"您不是当前单据的指定审核人！");
			}
			if(yxQcysOrder.getShenhe()==1){
				return Result.error(500,"单据已审核，请勿重复操作!");
			}
			yxQcysOrder.setShenhe(1);
			yxQcysOrder.setShTime(new Date());
			yxQcysOrderService.updateById(yxQcysOrder);
			return Result.OK("审核成功!");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}

	/**
	 *  弃审
	 *
	 * @param yxQcysOrder
	 * @return
	 */
	@AutoLog(value = "期初应收款单-弃审")
	@ApiOperation(value="期初应收款单-弃审", notes="期初应收款单-弃审")
	@PostMapping(value = "/unCheckYsOrder")
	public Result<?> unCheckYsOrder(@RequestBody  YxQcysOrder yxQcysOrder) {
		try{
			// 获取登录用户信息
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			if(!sysUser.getId().equals(yxQcysOrder.getSaleman())){
				return Result.error(500,"您不是当前单据的指定审核人！");
			}
			if(yxQcysOrder.getShenhe()==0){
				return Result.error(500,"单据已弃审，请勿重复操作!");
			}
			yxQcysOrder.setShenhe(0);
			yxQcysOrder.setShTime(null);
			yxQcysOrderService.updateById(yxQcysOrder);
			return Result.OK("弃审成功!");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}
	/**
	 *  编辑
	 *
	 * @param yxQcysOrder
	 * @return
	 */
	@AutoLog(value = "期初应收款单-编辑")
	@ApiOperation(value="期初应收款单-编辑", notes="期初应收款单-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody YxQcysOrder yxQcysOrder) {
		try{
			if(yxQcysOrder.getShenhe()==1){
				return Result.error(500,"单据已审核，无法编辑！");
			}
			yxQcysOrder.setSurplusJine(yxQcysOrder.getQcysje());
			yxQcysOrderService.updateById(yxQcysOrder);
			return Result.OK("编辑成功!");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "期初应收款单-通过id删除")
	@ApiOperation(value="期初应收款单-通过id删除", notes="期初应收款单-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		try{
			YxQcysOrder yxQcysOrder = yxQcysOrderService.getById(id);
			if(yxQcysOrder.getShenhe()==1){
				return Result.error(500,"单据已审核，无法删除！");
			}
			yxQcysOrderService.removeById(id);
			return Result.OK("删除成功!");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "期初应收款单-批量删除")
	@ApiOperation(value="期初应收款单-批量删除", notes="期初应收款单-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.yxQcysOrderService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "期初应收款单-通过id查询")
	@ApiOperation(value="期初应收款单-通过id查询", notes="期初应收款单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		YxQcysOrder yxQcysOrder = yxQcysOrderService.getById(id);
		if(yxQcysOrder==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(yxQcysOrder);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param yxQcysOrder
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YxQcysOrder yxQcysOrder) {
        return super.exportXls(request, yxQcysOrder, YxQcysOrder.class, "期初应收款单");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YxQcysOrder.class);
    }

}
